package com.google.zxing.journeyapps.barcodescanner;

import ohos.app.Context;
import ohos.sensor.agent.CategoryOrientationAgent;
import ohos.sensor.bean.CategoryOrientation;
import ohos.sensor.data.CategoryOrientationData;
import ohos.sensor.listener.ICategoryOrientationDataCallback;

/**
 * Hack to detect when screen rotation is reversed, since that does not cause a configuration change.
 *
 * If it is changed through something other than the sensor (e.g. programmatically), this may not work.
 *
 * See http://stackoverflow.com/q/9909037
 */
public class RotationListener {
    private int lastRotation;

    private ICategoryOrientationDataCallback orientationEventListener;
    private RotationCallback callback;
    private int matrix_length = 9;
    private int rotationVectorLength = 9;
    private CategoryOrientationAgent categoryOrientationAgent;

    public RotationListener() {
    }

    public void listen(Context context, RotationCallback callback) {
        // Stop to make sure we're not registering the listening twice.
        stop();

        // Only use the ApplicationContext. In case of a memory leak (e.g. from a framework bug),
        // this will result in less being leaked.
        context = context.getApplicationContext();

        this.callback = callback;

        categoryOrientationAgent = new CategoryOrientationAgent();
        CategoryOrientation categoryOrientation = categoryOrientationAgent.getSingleSensor(CategoryOrientationAgent.SENSOR_CATEGORY_ORIENTATION);

        Context finalContext = context;
        orientationEventListener = new ICategoryOrientationDataCallback() {
            @Override
            public void onSensorDataModified(CategoryOrientationData categoryOrientationData) {

                int dim = categoryOrientationData.getSensorDataDim();
                float degree = categoryOrientationData.getValues()[0];
                float[] rotationMatrix = new float[matrix_length];
                CategoryOrientationData.getDeviceRotationMatrix(rotationMatrix, categoryOrientationData.values); // 根据旋转矢量传感器的数据获得旋转矩阵
                float[] rotationAngle = new float[rotationVectorLength];
                rotationAngle = CategoryOrientationData.getDeviceOrientation(rotationMatrix, rotationAngle); // 根据计算出来的旋转矩阵获取设备的方向

                RotationCallback localCallback = RotationListener.this.callback;
                    int newRotation = finalContext.getDisplayOrientation();
                    if (newRotation != lastRotation) {
                        lastRotation = newRotation;
                        localCallback.onRotationChanged(newRotation);
                    }
//                }
            }

            @Override
            public void onAccuracyDataModified(CategoryOrientation categoryOrientation, int i) {

            }

            @Override
            public void onCommandCompleted(CategoryOrientation categoryOrientation) {

            }
        };
        categoryOrientationAgent.setSensorDataCallback(orientationEventListener,categoryOrientation, 2000L);


        lastRotation = context.getDisplayOrientation();
    }

    public void stop() {
        // To reduce the effect of possible leaks, we clear any references we have to external
        // objects.
        if (this.orientationEventListener != null) {
            categoryOrientationAgent.releaseSensorDataCallback(orientationEventListener);
        }
        this.orientationEventListener = null;
        this.callback = null;
    }
}
